package com.sgb.kjwl.viewmodel.self.company
import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.sgb.kjwl.api.APIManager
import com.sgb.kjwl.model.entity.self.AgentCentralizedEntity
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.api.ICallBack
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.base.model.entity.H5ReEntity
import com.swgk.core.base.model.entity.ListResponse
import retrofit2.Call

class CentralizedMiningViewModel : BaseViewModel() {

    private var page = 1//分页加载起始页
    private var limit = 10 //每页加载数量
    private var map : HashMap<String, Any> = HashMap()

    val joinList = MutableLiveData<List<AgentCentralizedEntity>>()

    val offerList = MutableLiveData<List<AgentCentralizedEntity>>()

    val readPosition = MutableLiveData<Int>()

    //加盟单位列表
    fun getCentralizedJoinList(isLoadMore : Boolean){
        page = if (isLoadMore) page + 1 else 1
        map["page"] = page
        map["limit"] = limit
        APIManager.getInstance().userAPI.getCentralizedJoinList(map).enqueue(object :
            ICallBack<BaseEntity<ListResponse<List<AgentCentralizedEntity>>>>(true, true) {
            override fun onSuccess(data: BaseEntity<ListResponse<List<AgentCentralizedEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null){
                    joinList.postValue(data.data.list)
                } else {
                    joinList.postValue(ArrayList())
                }
            }

            override fun onFailure(
                call: Call<BaseEntity<ListResponse<List<AgentCentralizedEntity>>>>,
                t: Throwable
            ) {
                super.onFailure(call, t)
                joinList.postValue(ArrayList())
            }
        })
    }

    //报价单位列表
    fun getCentralizedOfferList(isLoadMore : Boolean){
        page = if (isLoadMore) page + 1 else 1
        map["page"] = page
        map["limit"] = limit
        APIManager.getInstance().userAPI.getCentralizedOfferList(map).enqueue(object :
            ICallBack<BaseEntity<ListResponse<List<AgentCentralizedEntity>>>>(true, true) {
            override fun onSuccess(data: BaseEntity<ListResponse<List<AgentCentralizedEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null){
                    offerList.postValue(data.data.list)
                } else {
                    offerList.postValue(ArrayList())
                }
            }

            override fun onFailure(
                call: Call<BaseEntity<ListResponse<List<AgentCentralizedEntity>>>>,
                t: Throwable
            ) {
                super.onFailure(call, t)
                offerList.postValue(ArrayList())
            }
        })
    }

    //保存用户已读数据
    fun saveUserReadAgent(id : String, position : Int){
        map = HashMap()
        map["businessNo"] = id
        APIManager.getInstance().userAPI.saveUserReadAgent(map).enqueue(object : ICallBack<BaseEntity<*>>(){
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data != null){
                    readPosition.postValue(position)
                }
            }
        })
    }

    //需方发起和企业专项
    /**
     * type 区分供方发起还是需方发起
     * id 需方发起的id 供方发起的no
     */
    fun getH5Url(context: Context, id: String, status: Int, type : Int?) {
        var detail = if (type != null && type == 3) "Factorydetail" else "detail"
        APIManager.getInstance().pickAPI.getUrlSetting(detail).enqueue(object : ICallBack<BaseEntity<H5ReEntity>>(true, true){
            override fun onSuccess(data: BaseEntity<H5ReEntity>?) {
                if (data != null && data.data != null){
                    val url = data.data.url + if (type == 3) "?id=$id" else "?id=$id&type=$status"
                    WebViewX5HostActivity.start(context, url)
                }
            }

        })
    }

}